package com.machineapps.botesom.listener;

import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

import com.machineapps.framework.util.AnimationHelper;

public class FlingCategoryGestureDetectorListener extends SimpleOnGestureListener {

	private static final int SWIPE_MIN_DISTANCE = 120;

	private static final int SWIPE_THRESHOLD_VELOCITY = 200;

	private ViewFlipper viewFlipper;

	public FlingCategoryGestureDetectorListener(ViewFlipper viewFlipper) {
		this.viewFlipper = viewFlipper;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

		if (e1 != null && e2 != null) {
			if (e1.getX() > e2.getX() && Math.abs(e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				this.viewFlipper.setInAnimation(AnimationHelper.inFromRightAnimation());
				this.viewFlipper.setOutAnimation(AnimationHelper.outToLeftAnimation());

				if (this.viewFlipper.getDisplayedChild() < this.viewFlipper.getChildCount() - 1) {
					this.viewFlipper.showNext();
				}
			} else if (e1.getX() < e2.getX() && e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				this.viewFlipper.setInAnimation(AnimationHelper.inFromLeftAnimation());
				this.viewFlipper.setOutAnimation(AnimationHelper.outToRightAnimation());

				if (this.viewFlipper.getDisplayedChild() != 0) {
					this.viewFlipper.showPrevious();
				}
			}
		}
		return true;
	}
}